/*
 *  Cero Project - Copyright   2006 The Cero Developement Team 
 *  (Michael Laguerre, Camille Roux, Matthieu Segret, Mathieu Sivade) 
 * 
 *  This program is free software; you can redistribute it and/or modify it 
 *  under the terms of the GNU General Public License as published by the Free 
 *  Software Foundation; either version 2 of the License, or (at your option) 
 *  any later version.
 * 
 *  This program is distributed in the hope that it will be useful, but 
 *  WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY 
 *  or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 
 *  for more details.
 */

package org.ceroproject.ui.commandline.commands;

import org.ceroproject.games.Game;
import org.ceroproject.games.GameInitializer;
import org.ceroproject.games.Rule;
import org.ceroproject.plugin.PluginManager;
import org.ceroproject.ui.commandline.Command;

public class Start extends Command {
	private static Start instance = new Start();

	protected Start() {
		super("start");
	}

	@Override
	public String toString() {
		return "start a game";
	}

	public static Start getCommand() {
		return instance;
	}

	@Override
	public boolean executer(String parametres) {
		if (parametres.trim() == "")
			return false;
		if (parametres.equals("bataille")) {
			PluginManager pmanager = PluginManager.getInstance();
			Game game = pmanager.getNewGame("Cero official bataille");
			for (Rule r : pmanager.getGameRules(game.getGameName()))
				game.getRules().add(r);

			// 1 joueur humain suffira
			for (int i = 0; i < 1; i++)
				game.getPlayers().add(pmanager.getNewPlayer(game.getGameName()));
			// une AI de chaque
			for (String aip : pmanager.getAIsName(game.getGameName()))
				game.getPlayers().add(pmanager.getNewAI(game.getGameName(), aip));

			for (GameInitializer init : pmanager.getGameInitializers(game.getGameName()))
				game.getGameInitializers().add(init);
			try {
				game.startGame();
			} catch (Exception e) {
				System.out.println(e.getMessage());
				e.printStackTrace();
				return false;
			}
			return true;
		} else if (parametres.equals("uno")) {
			PluginManager pmanager = PluginManager.getInstance();
			Game game = pmanager.getNewGame("Cero official uno");
			for (Rule r : pmanager.getGameRules(game.getGameName()))
				game.getRules().add(r);
			
			// 1 joueur humain suffira
			for (int i = 0; i < 1; i++)
				game.getPlayers().add(pmanager.getNewPlayer(game.getGameName()));
			// 3 AI de chaque
			for (String aip : pmanager.getAIsName(game.getGameName()))
				for (int i = 0; i < 3; i++)
					game.getPlayers().add(pmanager.getNewAI(game.getGameName(), aip));
			
			for (GameInitializer init : pmanager.getGameInitializers(game.getGameName()))
				game.getGameInitializers().add(init);

			try {
				game.startGame();
			} catch (Exception e) {
				System.out.println(e.getMessage());
				e.printStackTrace();
				return false;
			}
			return true;
		}
		System.out.println("Le jeu \"" + parametres + "\" est inconnu.");
		return false;
	}

	@Override
	public String help() {
		return "starts the given game";
	}

}
